package com.tripspins.app.bo;

import android.content.Context;
import android.location.Address;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.format.DateFormat;

import com.tripspins.app.helper.FILE_TYPE;
import com.tripspins.app.helper.FileHelper;
import com.google.android.gms.maps.model.LatLng;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Random;


public class Pin implements Parcelable {
    private boolean isSynced;
    /**
     *
     */

    private long id;
    private long tripId;
    private String title;


    private String textNote;
    private Date date;
    private ArrayList<Attachment> attachments;
    private CurrentWeather currentWeather;
    private Address address;
    private LatLng latLng;
    private Uri randomPicture;

    public Pin()
    {
        id = -1;
        attachments = new ArrayList<Attachment>();
        currentWeather = new CurrentWeather();
        date = new Date();
        address = new Address(Locale.getDefault());
        isSynced = false;
        latLng = null;


    }
    public boolean isSynced() {
        return isSynced;
    }

    public void setSynced(boolean synced) {
        isSynced = synced;
    }
    @Override
    public String toString() {
        return "Pin [id=" + id + ", title=" + title + ", latitude="
                + latLng.latitude + ", longitude=" + latLng.longitude + ", date=" + date
                + ", attachments=" + attachments.size() + "]";
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getTripId() {
        return tripId;
    }

    public void setTripId(long tripId) {
        this.tripId = tripId;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getLatitude() {
        return latLng.latitude;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }

    public double getLongitude() {
        return latLng.longitude;
    }

    public Date getDate() {
        return date;
    }
    public String getDateFormatted()
    {
        return DateFormat.format("dd/MM/yyyy HH:mm:ss", date).toString();
    }

    public String getDateFormatted(String format)
    {
        return DateFormat.format(format, date).toString();
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public ArrayList<Attachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(ArrayList<Attachment> attachments) {
        this.attachments = attachments;
    }

    public CurrentWeather getCurrentWeather() {
        return currentWeather;
    }

    public void setCurrentWeather(CurrentWeather currentWeather) {
        this.currentWeather = currentWeather;
    }

    public void addAttachment(Attachment att)
    {
        attachments.add(att);
    }
    public LatLng getLatLng()
    {
        return latLng;
    }

    public String getTitle()
    {
        return title;
    }
    public String getSnippet() {
        StringBuilder sb = new StringBuilder();
        //sb.append(address.getAddressLine(0));
        if (address != null)
        {
            if (address.getCountryName() != null)
                sb.append(address.getCountryName() + "\n");
            if (address.getLocality() != null)
                sb.append(address.getLocality() + "\n");

        }

        sb.append(DateFormat.format("dd/MM/yyyy \nHH:mm:ss", date).toString());
        sb.append("\n");
        sb.append(getAttachments().size());
        sb.append(" Attachments");


        return sb.toString();
    }

    public void setAddress(Address address) {
        this.address = address;
    }
    public Address getAddress() {
        return address;
    }
    public String getTextNote() {
        return textNote;
    }

    public void setTextNote(String textNote) {
        this.textNote = textNote;
    }

    public String getAddressString(){

        String strAddress = "";
        if(address == null) return strAddress;

        if(address.getLocality()==null)
        {
            if(address.getSubLocality() != null)
                strAddress += address.getSubLocality();
        }else strAddress += address.getLocality();

        if(address.getAdminArea() != null) strAddress += "\n" + address.getAdminArea();

        if(address.getCountryCode()==null)
        {
            if(address.getCountryName() != null)
                strAddress += "\n" + address.getCountryName();
        }else strAddress += "\n" + address.getCountryCode();


        return strAddress;
    }


    public File getWeatherIconUri(Context context, long tripId)
    {
        FileHelper fileHelper = new FileHelper(context);
        File dir =  fileHelper.getAttachmentsDirectory(tripId, id);
        File icon = new File(dir.getPath().concat("/WEATHER_ICON.JPG"));
        return icon;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(id);
        parcel.writeLong(tripId);
        parcel.writeString(title);
        parcel.writeString(textNote);
        parcel.writeDouble(latLng.latitude);
        parcel.writeDouble(latLng.longitude);
        parcel.writeString(DateFormat.format("dd/MM/yyyy HH:mm:ss", date).toString());
        parcel.writeTypedList(attachments);
        parcel.writeParcelable(currentWeather, i);
        parcel.writeParcelable(address,i);

    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Pin createFromParcel(Parcel parcel) {
            return new Pin(parcel);
        }

        public Pin[] newArray(int size) {
            return new Pin[size];
        }
    };

    private Pin(Parcel parcel)
    {
        id = parcel.readLong();
        tripId = parcel.readLong();
        title = parcel.readString();
        textNote = parcel.readString();
        double latitude = parcel.readDouble();
        double longitude = parcel.readDouble();
        latLng = new LatLng(latitude,longitude);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        try {
            date = format.parse(parcel.readString());
        } catch (Exception ex) {
            date = null;
        }
        attachments = new ArrayList<Attachment>();
        parcel.readTypedList(attachments,Attachment.CREATOR);
        currentWeather = parcel.readParcelable(CurrentWeather.class.getClassLoader());
        address = parcel.readParcelable(Address.class.getClassLoader());

    }




    public String getRandomImagePath() {
        int maxIndex = attachments.size() -1;
        int randomIndex = 0;

        if (attachments.size() == 0) return null;

        if(maxIndex > 0){
            Random rand = new Random();
            randomIndex = rand.nextInt(maxIndex);
        }
        if(attachments.get(randomIndex).getFileType() == FILE_TYPE.CONTACT )
            return attachments.get(randomIndex).getContact().getPicturePath();
        return attachments.get(randomIndex).getThumbnailPath();

    }

    public Uri getRandomImageUri() {
        int maxIndex;
        int randomIndex = 0;
        ArrayList<Attachment> images = new ArrayList<Attachment>();
        if (attachments.size() == 0) return null;
        for (Attachment att : attachments)
        {
            if (att.getFileType()==FILE_TYPE.IMAGE || att.getFileType()==FILE_TYPE.VIDEO)
                images.add(att);
        }
        maxIndex = images.size();
        if(maxIndex == 0)
            return null;

        Random rand = new Random();
        randomIndex = rand.nextInt(maxIndex);
        return Uri.parse(images.get(randomIndex).getThumbnailPath());

    }

    public ArrayList<Attachment> getAttachmentsToSync() {
        ArrayList<Attachment> atts = new ArrayList<Attachment>();
        for(Attachment a : attachments)
        {
            if( a.getFileType() == FILE_TYPE.IMAGE ||a.getFileType() == FILE_TYPE.VIDEO )
            {
                atts.add(a);
            }
        }
        return atts;

    }

    public Uri getFirstImageUri() {
        for (Attachment att : attachments) {
            if (att.getFileType() == FILE_TYPE.IMAGE || att.getFileType() == FILE_TYPE.VIDEO)
                return Uri.parse(att.getThumbnailPath());
        }
        return  null;
    }
    public String getFirstImageString() {
        for (Attachment att : attachments) {
            if (att.getFileType() == FILE_TYPE.IMAGE || att.getFileType() == FILE_TYPE.VIDEO)
                return att.getThumbnailPath();
        }
        return  null;
    }
}
